﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Exceptions;
using WmsApp.Lookup;
using WmsApp.Service;
using WmsApp.StaticData;
using WmsApp.WmsService;
using Binding = WmsApp.UI.DataBinding.Binding;

namespace WmsApp.Window.Editor
{
    public partial class FormGoodsCodeTypeSmallEditor : Form
    {
        private readonly GoodsCodeType _goodsCodeType;
        private readonly bool _isEditing;

        /// <summary>
        ///   修改物料代码调用
        /// </summary>
        /// <param name = "goodsCodeType"></param>
        public FormGoodsCodeTypeSmallEditor(GoodsCodeType goodsCodeType)
        {
            _isEditing = true;
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                goodsCodeType.WcfEntityState = EntityState.Modified;
                //代码不能修改
                textEditNewGoodsCode.Properties.ReadOnly = true;

                Binding.BindUnit(lookUpEditUnitCode, textEditUnitName, textEditUnitName.Tag.ToString());

                Binding.BindStoreType(lookUpEditStoreTypeCode, textEditStoreTypeName,
                                      textEditStoreTypeName.Tag.ToString());

                LookupBind.BindingLookUpEditSystemUser(lookUpEditChargeID, textEditChargeName,
                                                       textEditChargeName.Tag.ToString());

                _goodsCodeType = goodsCodeType;
                foreach (var control in groupControl1.Controls)
                {
                    if (control is BaseEdit)
                    {
                        var edit = (BaseEdit) control;
                        edit.DataBindings.Clear();
                        edit.DataBindings.Add("EditValue", goodsCodeType, edit.Tag.ToString());
                    }
                }
            }
        }

        public FormGoodsCodeTypeSmallEditor()
        {
            _isEditing = false;
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                Binding.BindUnit(lookUpEditUnitCode, textEditUnitName, textEditUnitName.Tag.ToString());

                Binding.BindStoreType(lookUpEditStoreTypeCode, textEditStoreTypeName,
                                      textEditStoreTypeName.Tag.ToString());

                LookupBind.BindingLookUpEditSystemUser(lookUpEditChargeID, textEditChargeName,
                                                       textEditChargeName.Tag.ToString());

                _goodsCodeType = GoodsCodeType.CreateGoodsCodeType("请输入新物料代码");
                foreach (var control in groupControl1.Controls)
                {
                    if (control is BaseEdit)
                    {
                        var edit = (BaseEdit) control;
                        edit.DataBindings.Clear();
                        edit.DataBindings.Add("EditValue", _goodsCodeType, edit.Tag.ToString());
                    }
                }
            }
        }

        public GoodsCodeType FGoodsCodeType
        {
            get { return _goodsCodeType; }
        }

        private void CmdSave3_Click(object sender, EventArgs e)
        {
            try
            {
                if (lookUpEditChargeID.EditValue == null ||
                    String.IsNullOrEmpty(lookUpEditChargeID.EditValue.ToString().Trim()))
                {
                    XtraMessageBox.Show("必须选择责任人！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (String.IsNullOrEmpty(textEditNewGoodsCode.Text.Trim())
                    || String.IsNullOrEmpty(textEditNewGoodsName.Text.Trim()))
                {
                    XtraMessageBox.Show("新物料代码和物料名称都必须输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var list = new List<GoodsCodeType> {_goodsCodeType};
                EntityServiceFactory.GetEntityService<GoodsCodeType, GoodsCodeTypeService>().SaveEntityList(list);
                if (! _isEditing)
                {
                    StaticObjects.GetGoodsCodeTypes().Add(_goodsCodeType);
                }

                //通知所有观察者该表状态，无需从服务器更新，否则需要耗费很多性能资源
                GoodsCodeTypeSubjectFactory.GetGoodsCodeTypeSubject().Nofify(false);

                XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }
    }
}